package com.ylx.service;

import com.ylx.mapper.GoodsMapper;
import com.ylx.pojo.Goods;
import com.ylx.pojo.PageBean;
import com.ylx.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<Goods> selectAll () {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.selectAll();
        sqlSession.close();

        return goods;

    }

    @Override
    public void add (Goods goods) {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        goodsMapper.add(goods);
        sqlSession.commit();
        sqlSession.close();
        return;


    }

    @Override
    public void update (Goods goods) {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        goodsMapper.update(goods);
        sqlSession.commit();
        sqlSession.close();
        return;
    }

    @Override
    public void delete (int[] ids) {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        goodsMapper.delete(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public PageBean<Goods> selectByPage (int currentPage, int PageSize) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
//     计算出开始的索引 其中currentPage是当前的页码，PageSize是当前页面的条数
        int begin = (currentPage - 1) * PageSize;
//        计算出条数
        int size = PageSize;
        //查找出当前的页面的数据
        List<Goods> rows = goodsMapper.selectByPage(begin, size);
//        查找总数
        int totalCount = goodsMapper.totalCount();
//        封装对象
        PageBean<Goods> pageBean = new PageBean<>();

        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();
        return pageBean;

    }

    @Override
    public PageBean<Goods> selectByPageCondition (int currentPage, int PageSize, Goods goods) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
//     计算出开始的索引 其中currentPage是当前的页码，PageSize是当前页面的条数
        int begin = (currentPage - 1) * PageSize;
//        计算出条数
        int size = PageSize;
        //查找出当前的页面的数据
//        处理参数
        String goodsName = goods.getGoodsName();
        if (goodsName != null && goodsName.length() > 0) {
            goods.setGoodsName("%" + goodsName + "%");
        }
        String categoryId = goods.getCategoryId();
        if (categoryId != null && categoryId.length() > 0) {
            goods.setCategoryId("%" + categoryId + "%");
        }

        List<Goods> rows = goodsMapper.selectByPageCondition(begin, size, goods);
//        查找总数
        int totalCount = goodsMapper.totalCountCondition(goods);
//        封装对象
        PageBean<Goods> pageBean = new PageBean<>();

        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();
        return pageBean;
    }

    @Override
    public List<Goods> getProducts () {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.getProducts();
        sqlSession.close();
        return goods;
    }

    @Override
    public Goods getProductsById (String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.getProductsById(id);
        sqlSession.close();
        return goods;
    }
    @Override
    public Goods getProductsById1 (String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.getProductsById1(id);
        sqlSession.close();
        return goods;
    }

    @Override
    public void addMenu (Goods goods) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        goodsMapper.addMenu(goods);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void addRecipe (Goods goods) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        goodsMapper.addRecipe(goods);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public int deleteMenu (String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        int i = goodsMapper.deleteMenu(id);

        sqlSession.commit();

        sqlSession.close();

        return i;
    }

    @Override
    public void updateStatusById (String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        goodsMapper.updateStatusById(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Goods> selectByCondition (String goodsName){

        SqlSession sqlSession = sqlSessionFactory.openSession();

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        if (goodsName != null && goodsName.length() > 0) {
            goodsName=("%" + goodsName + "%");
        }
        List<Goods> good= goodsMapper.selectByCondition(goodsName);

          sqlSession.close();
          return  good;

    }

    @Override
    public List<Goods> selectByCondition1 (String goodsName) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        if (goodsName != null && goodsName.length() > 0) {
            goodsName=("%" + goodsName + "%");
        }
        List<Goods> goods = goodsMapper.selectByCondition1(goodsName);
        return goods;
    }

    @Override
    public void updateRecipe (Goods goods) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        goodsMapper.updateRecipe(goods);
        sqlSession.commit();
        sqlSession.close();


    }


}
